{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 124. Binary Tree Maximum Path Sum\n",
    "Given a non-empty binary tree, find the maximum path sum.\n",
    "\n",
    "For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along\n",
    "the parent-child connections. The path must contain at least one node and does not need to go through the root.\n",
    "\n",
    "    Example 1:\n",
    "        Input: [1,2,3]\n",
    "\n",
    "               1\n",
    "              / \\\n",
    "             2   3\n",
    "\n",
    "        Output: 6\n",
    "\n",
    "    Example 2:\n",
    "        Input: [-10,9,20,null,null,15,7]\n",
    "\n",
    "           -10\n",
    "           / \\\n",
    "          9  20\n",
    "            /  \\\n",
    "           15   7\n",
    "\n",
    "        Output: 42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# Definition for a binary tree node.\n",
    "class TreeNode:\n",
    "    def __init__(self, x):\n",
    "        self.val = x\n",
    "        self.left = None\n",
    "        self.right = None\n",
    "\n",
    "class Solution:\n",
    "    def __init__(self):\n",
    "        self.maxSum = float('-inf')\n",
    "\n",
    "    def maxPathSum(self, root: TreeNode) -> int:\n",
    "        def maxGain(node):\n",
    "            if not node:\n",
    "                return 0\n",
    "            leftGain = max(maxGain(node.left), 0)\n",
    "            rightGain = max(maxGain(node.right), 0)\n",
    "            priceNewpath = node.val + leftGain + rightGain\n",
    "\n",
    "            self.maxSum = max(self.maxSum, priceNewpath)\n",
    "\n",
    "            return node.val + max(leftGain, rightGain)\n",
    "\n",
    "        maxGain(root)\n",
    "        return self.maxSum\n",
    "\n",
    "S = Solution()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "x = [1, 2, 3]\n",
    "root = TreeNode(x[0])\n",
    "root.left = TreeNode(x[1])\n",
    "root.right = TreeNode(x[2])\n",
    "\n",
    "print(S.maxPathSum(root))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n"
     ]
    }
   ],
   "source": [
    "x = [-10, 9, 20, None, None, 15, 7]\n",
    "root = TreeNode(x[0])\n",
    "root.left = TreeNode(x[1])\n",
    "root.right = TreeNode(x[2])\n",
    "root.right.left = TreeNode(x[5])\n",
    "root.right.right = TreeNode(x[6])\n",
    "\n",
    "print(S.maxPathSum(root))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}